package api

import (
	"github.com/gin-gonic/gin"
	"github.com/opentracing-contrib/go-gin/ginhttp"
	"github.com/opentracing/opentracing-go"
	zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
	"github.com/openzipkin/zipkin-go"
	reporter "github.com/openzipkin/zipkin-go/reporter/http"
	"net/http"
)

// opentracingMiddleware 上报中间件 上报地址 http://127.0.0.1:9411/api/v2/spans
func opentracingMiddleware() gin.HandlerFunc {
	// 创建Reporter 上报节点
	report := reporter.NewReporter("http://127.0.0.1:9411/api/v2/spans")
	// 创建本地节点
	endpoint, err := zipkin.NewEndpoint("content_system", "127.0.0.1:8080")
	if err != nil {
		return nil
	}

	// 创建Zipkin Tracer
	tracer, err := zipkin.NewTracer(report, zipkin.WithLocalEndpoint(endpoint),
		zipkin.WithTraceID128Bit(true))

	if err != nil {
		panic(any(err))
	}

	zipTracer := zipkinot.Wrap(tracer)
	opentracing.SetGlobalTracer(zipTracer)

	// 创建中间件

	return ginhttp.Middleware(zipTracer, ginhttp.OperationNameFunc(func(r *http.Request) string {
		return r.URL.Path
	}))

}
